package com.roads.site.service.impl;

import com.roads.common.utils.DateUtils;
import com.roads.common.utils.SecurityUtils;
import com.roads.site.domain.SiteVideo;
import com.roads.site.domain.SiteVideoUser;
import com.roads.site.mapper.SiteVideoMapper;
import com.roads.site.mapper.SiteVideoUserMapper;
import com.roads.site.service.ISiteVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 视频监控Service业务层处理
 * 
 * @author nhp
 * @date 2024-09-12
 */
@Service
public class SiteVideoServiceImpl implements ISiteVideoService {

    @Autowired
    private SiteVideoMapper mapper;

    @Autowired
    private SiteVideoUserMapper siteVideoUserMapper;

    /**
     * 根据主键ID查询详情
     * 
     * @param id 视频监控主键
     * @return 视频监控
     */
    @Override
    public SiteVideo getModelById(Integer id) {
        return mapper.getModelById(id);
    }

    /**
     * 查询用户场景视频监控列表
     *
     * @param user 用户
     * @param type 场景
     * @return 视频监控集合
     */
    @Override
    public List<SiteVideo> selectUserVideoListByType(String user, Integer type) {
        return mapper.selectUserVideoListByType(user, type);
    }

    /**
     * 查询列表
     * 
     * @param model 视频监控
     * @return 视频监控
     */
    @Override
    public List<SiteVideo> selectModelList(SiteVideo model) {
        return mapper.selectModelList(model);
    }

    /**
     * 查询用户视频列表
     *
     * @param model 用户视频监控
     * @return 用户视频监控集合
     */
    @Override
    public List<SiteVideoUser> selectUserVideoList(SiteVideoUser model) {
        return siteVideoUserMapper.selectModelList(model);
    }

    /**
     * 查询用户未选择的视频监控
     *
     * @param model 视频监控
     * @return 视频监控集合
     */
    @Override
    public List<SiteVideo> selectUserVideoUnselectedList(SiteVideo model) {
        return mapper.selectUserVideoUnselectedList(model);
    }

    /**
     * 新增
     * 
     * @param model 视频监控
     * @return 结果
     */
    @Override
    public int insert(SiteVideo model) {
        model.setCreateTime(DateUtils.getNowDate());
        return mapper.insert(model);
    }

    /**
     * 修改
     * 
     * @param model 视频监控
     * @return 结果
     */
    @Override
    public int update(SiteVideo model) {
        model.setUpdateTime(DateUtils.getNowDate());
        return mapper.update(model);
    }

    /**
     * 批量删除
     * 
     * @param ids 主键集合
     * @return 结果
     */
    @Override
    public int deleteByIds(Integer[] ids) {
        return mapper.deleteByIds(ids);
    }

    /**
     * 删除
     * 
     * @param id 视频监控主键
     * @return 结果
     */
    @Override
    public int delete(Integer id) {
        return mapper.delete(id);
    }

    /**
     * 保存用户选择的视频监控
     *
     * @param type 应用场景
     * @param videoIds 视频ID集合
     * @return 结果
     */
    @Override
    public int saveUserVideo(Integer type, String videoIds) {
        String user = SecurityUtils.getUsername();
        int orderNum = siteVideoUserMapper.getMaxOrderNumByUserAndType(type, user);
        String[] videoArray = videoIds.split(",");
        List<SiteVideoUser> list = new ArrayList<>();
        if (videoArray.length >0) {
            for (int i = 0; i < videoArray.length; i++) {
                orderNum++;
                SiteVideoUser videoUser = new SiteVideoUser();
                videoUser.setVideoId(Integer.parseInt(videoArray[i]));
                videoUser.setUser(user);
                videoUser.setType(type);
                videoUser.setOrderNum(orderNum);
                list.add(videoUser);
            }
        }
        siteVideoUserMapper.insertBatch(list);
        return 1;
    }

    /**
     * 视频监控上移下移
     *
     * @param id 自增主键
     * @param type 上移下移
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int moveVideo(Integer id, String type) {
        SiteVideoUser moveVideo1 = siteVideoUserMapper.getModelById(id);
        String user = SecurityUtils.getUsername();
        int orderNum = moveVideo1.getOrderNum();
        if ("up".equals(type)) {
            orderNum--;
        } else {
            orderNum++;
        }
        SiteVideoUser moveVideo2 = siteVideoUserMapper.getModelByUserAndTypeAndOrderNum(user, moveVideo1.getType(), orderNum);
        moveVideo2.setOrderNum(moveVideo1.getOrderNum());
        moveVideo1.setOrderNum(orderNum);
        siteVideoUserMapper.update(moveVideo1);
        siteVideoUserMapper.update(moveVideo2);
        return 1;
    }
}
